package com.bnlr.lrparking.appui.bank;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.bnlr.lrparking.appui.paypwd.SetPayPasswordActivity;
import com.bnlr.lrparking.app.Application;
import com.bnlr.lrparking.R;
import com.bnlr.lrparking.base.BaseActivity;
import com.bnlr.lrparking.app.AppConfig;
import com.bnlr.lrparking.constant.LrConstant;
import com.bnlr.lrparking.constant.Urls;
import com.bnlr.lrparking.event.UnBindBankEvent;
import com.bnlr.lrparking.model.BaseArrayBean;
import com.bnlr.lrparking.util.pwddialog.SecurityEditCompileListener;
import com.bnlr.lrparking.util.pwddialog.SecurityPasswordEditText;
import com.bnlr.nohttp.CallServer;
import com.bnlr.nohttp.CustomHttpListener;
import com.yolanda.nohttp.NoHttp;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 解绑银行卡
 */
public class UnBindBankActivity extends BaseActivity {
    private Unbinder unbinder;
    @BindView(R.id.tv_nav_title)
    TextView tvTitle;

    @BindView(R.id.security_pass)
    SecurityPasswordEditText editSecurityCode;

    private String bankId;
    private int inputCount = 6;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_un_bind_bank);

        unbinder = ButterKnife.bind(this);
        tvTitle.setText("解绑银行卡");
        Intent intent = getIntent();
        bankId = intent.getStringExtra("bank_id");
        editSecurityCode.getEditText(this);
        editSecurityCode.setSecurityEditCompileListener(new SecurityEditCompileListener() {
            @Override
            public void onNumCompleted(String num) {
                verifyPayPsw(num);
            }
        });
    }
    /**
     * 验证输入的支付密码是否正确
     * @param num
     */
    private void verifyPayPsw(String num) {
        mRequest = NoHttp.createStringRequest(Urls.NEW_IP, LrConstant.POST);
        mRequest.add("action", "DeleteUserBank");
        mRequest.add("phonePara", Application.DEVICE_ID);
        mRequest.add("uid", AppConfig.getInstance().getString("uid", ""));
        mRequest.add("id", bankId);
        mRequest.add("payPassword", num);
        CallServer.getRequestInstance().add(baseContext, mRequest,
                new CustomHttpListener<BaseArrayBean>(baseContext, true, BaseArrayBean.class) {
                    @Override
                    public void doWork(BaseArrayBean data, String code) {
                        if (code.equals("1")) {
                            //  成功
                            EventBus.getDefault().post(new UnBindBankEvent());
                            ToastUtils.showShort("解绑成功");
                            finish();
                        }
                    }
                    @Override
                    public void onFinally(JSONObject obj, String code, boolean isSucceed) {
                        if (!code.equals("1")) {
                            try {
                                /*if (obj.getString("msg").contains("支付密码错误")) {
                                    if (inputCount <= 1) {
                                        lockAccount();
                                    } else {
                                        payPwsFail();
                                    }

                                } else {*/
                                    ToastUtils.showShort(obj.getString("msg"));
                                editSecurityCode.clearEdit();
                               // }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }, true);
    }

    /**
     * 账号被锁
     */
    private void lockAccount() {
        AppConfig.getInstance().putBoolean("isLocked", true);
        View view3 = View.inflate(this,
                R.layout.setting_custom_dialog, null);
        // 对话框
        final Dialog dialog1 = new AlertDialog.Builder(this, R.style.NativeDialog)
                .create();
        dialog1.show();
        dialog1.getWindow().setWindowAnimations(R.style.dialog_anim_style);
        dialog1.getWindow().setContentView(view3);
        dialog1.setCancelable(false);
        dialog1.setCanceledOnTouchOutside(false);
        TextView tishi2 = (TextView) view3.findViewById(R.id.text_dialog);
        tishi2.setText("安全提示");
        TextView text_phone = (TextView) view3.findViewById(R.id.text_phone);
        text_phone.setText("支付密码输入错误次数过多，账户已被锁定，请点击忘记密码进行找回");
        // 取消按钮
        TextView btn_Cancel2 = (TextView) view3.findViewById(R.id.cancle);
        btn_Cancel2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog1.dismiss();
                finish();
            }
        });
        // 确定按钮
        TextView btn_OK2 = (TextView) view3.findViewById(R.id.sure);
        btn_OK2.setText("忘记密码");
        btn_OK2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog1.dismiss();
                Intent intent1 = new Intent(UnBindBankActivity.this,SetPayPasswordActivity.class);
                intent1.putExtra("reset","reset");
                startActivity(intent1);
            }
        });
    }

    private void payPwsFail() {
        View view3 = View.inflate(this,
                R.layout.setting_custom_dialog, null);
        // 对话框
        final Dialog dialog1 = new AlertDialog.Builder(this, R.style.NativeDialog)
                .create();
        dialog1.show();
        dialog1.getWindow().setWindowAnimations(R.style.dialog_anim_style);
        dialog1.getWindow().setContentView(view3);
        dialog1.setCancelable(false);
        dialog1.setCanceledOnTouchOutside(false);
        TextView tishi2 = (TextView) view3.findViewById(R.id.text_dialog);
        tishi2.setText("安全提示");
        TextView text_phone = (TextView) view3.findViewById(R.id.text_phone);
        inputCount--;
        text_phone.setText("支付密码错误，还可以输入" + inputCount + "次");
        // 取消按钮
        TextView btn_Cancel2 = (TextView) view3.findViewById(R.id.cancle);
        btn_Cancel2.setText("重新输入");
        btn_Cancel2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog1.dismiss();
                editSecurityCode.clearEdit();

            }
        });
        // 确定按钮
        TextView btn_OK2 = (TextView) view3.findViewById(R.id.sure);
        btn_OK2.setText("忘记密码");
        btn_OK2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog1.dismiss();
                Intent intent1 = new Intent(UnBindBankActivity.this,SetPayPasswordActivity.class);
                intent1.putExtra("reset","reset");
                startActivity(intent1);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    protected void onDestroy() {
        unbinder.unbind();
        super.onDestroy();
    }


}
